import requests
from lxml import etree
import re
import time
import random
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')
with open('a.txt', "a+") as ff:
    row = 0
    for y in range(305,306):
        time.sleep(random.randint(1, 100)/10)
        # url = "https://www.ruyile.com/xuexiao/?a=%s" % y
        url = "https://www.ruyile.com/xuexiao/?a=305&t=3"

        print(y)

        try:
            html = requests.get(url)
        except:
            print("等待10秒重试")
            time.sleep(10)
            html = requests.get(url)

        selector = etree.HTML(html.text)
        name = selector.xpath("/html/body/div[2]/div[6]/div[1]/div[4]/text()")
        
        try:
            page = re.search(r"\d+", name[-2]).group()
        except:
            page = 1
        page = eval(page)
        for p in range(1, page+1):
            time.sleep(random.randint(1, 100)/10)
            # u = "https://www.ruyile.com/xuexiao/?a=%s&p=%s" % (y, p)
            u = "https://www.ruyile.com/xuexiao/?a=305&t=3&p=%s" %  p

            print(u)

            try:
                html = requests.get(u)
            except:
                print("等待10秒重试")
                time.sleep(10)
                html = requests.get(u)

            selector = etree.HTML(html.text)
            ti = selector.xpath(
                "/html/body/div[2]/div[6]/div[1]/div[2]/div[1]/text()")
            ts = selector.xpath(
                '/html/body/div[2]/div[6]/div[1]/div[2]/div[@class="sk"]')
            for x in ts:
                t1 = x.xpath('./h4/a/text()')
                t2 = x.xpath('text()')
                dd = ti+t1+t2
                print(dd)
                ff.write("#".join(dd)+'\n')
                # ff.flush()
                row = row+1
                print(ti, row)
